<?php

require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 3;
$page_title = 'JANSEN';
$guide_message = 'Info | Tradeshows';
$out_ary = array();
$alert_message = '';

$key = "Enter company...";
getMyParam('action');
getMyParam('key');

// 
if ($action == "add" || $action == "edit") {
    getMyParam('tid');
    getMyParam('date');
	getMyParam('datezh');
    getMyParam('sdate');
    getMyParam('show');
    getMyParam('website');
    getMyParam('location');
    getMyParam('city');
    getMyParam('country');
    getMyParam('booth');
    getMyParam('company');
	getMyParam('showzh');
	getMyParam('locationzh');
	getMyParam('cityzh');
	getMyParam('countryzh');
	getMyParam('boothzh');
	getMyParam('companyzh');
    getMyParam('seq');
    
	//echo $city;
    if ($action == "edit" && (!is_numeric($tid) || $tid <= 0)) {
        $alert_message .= "\n\nFailed.";
    }

    if (trim($date) == "") {
        $alert_message .= "\n\nDate error";
    }
    
    if (trim($sdate) == "") {
        $alert_message .= "\n\nSequence Date error";
    }
    $unix_sdate = getUnixTime2($sdate);

    if ($alert_message == "") {
        if ($action == "add") {
			$sql = "INSERT INTO `tradeshows` (`date`,`datezh`, `sdate`, `show`, `website`, `location`, `city`, `country`, `booth`, `company`, `showzh`, `locationzh`, `cityzh`, `countryzh`, `boothzh`, `companyzh`,`seq`) VALUES ('$date','$datezh', '$unix_sdate', '$show', '$website', '$location', '$city', '$country', '$booth', '$company','$showzh', '$locationzh', '$cityzh', '$countryzh', '$boothzh', '$companyzh', '$seq')";
            $db->query($sql);
        }

        if ($action == "edit") {
            $sql = "UPDATE `tradeshows` SET
                        `date` = '$date',
						`datezh` = '$datezh',  
                        `sdate` = '$unix_sdate', 
                        `show` = '$show', 
                        `website` = '$website', 
                        `location` = '$location', 
                        `city` = '$city', 
                        `country` = '$country', 
                        `booth` = '$booth', 
                        `company` = '$company',
						`showzh` = '$showzh', 				 
						`locationzh` = '$locationzh', 
					    `cityzh` = '$cityzh', 
						`countryzh` = '$countryzh', 
						`boothzh` = '$boothzh', 
						`companyzh` = '$companyzh',
                        `seq` = '$seq'
                         WHERE `id` = '$tid'";
            
            $db->query($sql);
            

            goFromPage();
        }

        header("LOCATION:i_tradeshows.php");
        exit;
    }

}

if ($action == "del") {
    getMyParam('tid');

    if (is_numeric($tid) && $tid > 0) {

        $sql = "DELETE FROM `tradeshows` WHERE `id` = '" . $tid . "'";
        $db->query($sql);
        //header("LOCATION:i_tradeshows.php");
    }

    goFromPage();
}

//$sql_where = "WHERE `offline` > '$timestamp'";
$sql_where = "";

$page_url = 'i_tradeshows.php?';
if ($action == "search" && trim($key) != "") {
    $sql_where .= " WHERE `company` LIKE '%$key%' OR `show` LIKE '%$key%'";
    $page_url = 'i_tradeshows.php?action=search&key=' . $key . '&';
}


// *** page code start ***
$sql = "SELECT `id` FROM `tradeshows` $sql_where";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

$ppp = 1000;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

// *** page code end ***

$sql = "SELECT `id`, `date`,`datezh`, `show`, `website`, `location`, `country`, `city`, `booth`, `company`,`showzh`,`locationzh`, `countryzh`, `cityzh`, `boothzh`, `companyzh`, `seq` FROM `tradeshows` $sql_where ORDER BY seq ASC, id DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($key) != "") {
        $rs['company'] = str_replace($key, "<font color=red>$key</font>", $rs['company']);
        $rs['show'] = str_replace($key, "<font color=red>$key</font>", $rs['show']);
    }
    $out_ary[] = $rs;
}



$debug = 1;
debuginfo();

include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_i_tradeshows');
require_once PrintEot('a_footer');

?>